/*
 * 文件名：FeeNoteController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：huangxiaolang
 * 修改时间：2020年1月13日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.fia.web;


import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.fia.dao.FeeNoteDao;
import top.infopub.mgr.fia.domain.FeeNoteDomain;
import top.infopub.mgr.fia.service.FeeNoteService;
import top.infopub.mgr.fia.vo.FeeNoteVo;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.model.fia.FeeNote;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.hr.StaffInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import com.github.pagehelper.Page;
import com.google.common.collect.Maps;


@Controller
@RequestMapping("feeNote")
public class FeeNoteController {

    @Autowired
    private FeeNoteDao feeNoteDao;

    @Autowired
    private FeeNoteService feeNoteService;

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private OrgInfoService orgInfoService;

    @RequestMapping("list")
    public String list(HttpServletRequest request) {
        String bindOrgId = (String)SessionUserUtils.getSessionAttribute("bindOrgId");
        request.setAttribute("bindOrgId", bindOrgId);
        String visibleOrgIds = (String)SessionUserUtils.getSessionAttribute("visibleOrgIds");
        List<OrgInfo> orgInfos = new ArrayList<OrgInfo>();
        if (StringUtils.isEmpty(visibleOrgIds)) {
            Map<String, Object> where = Maps.newHashMap();
            where.put("state", 1);
            where.put("orderCondition", " types asc, add_time asc");
            orgInfos = orgInfoService.selectByMapWhere(where);
        }
        else {
            orgInfos = orgInfoService.selectByVisibleIds(visibleOrgIds);
        }
        request.setAttribute("orgInfos", orgInfos);
        return "fia/lay-feeNote-list";
    }

    @RequestMapping("queryDataByCondition")
    @ResponseBody
    public RspVo<FeeNoteDomain> queryDataByCondition(HttpServletRequest request,
                                                     FeeNoteVo feeNoteVo, ParamPage paramPage,
                                                     String startBeeDateWithoutDay,
                                                     String endBeeDateWithoutDay)
        throws ParseException {

        Page<FeeNoteDomain> page = new Page<FeeNoteDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        // 默认查询
        if(StringUtils.isEmpty(feeNoteVo.getHrOrgBillId())){
            feeNoteVo.setHrOrgBillId((String)SessionUserUtils.getSessionAttribute("bindOrgId"));
        }
        //查询时，结束日期增加一天
        if (feeNoteVo.getEndDate() != null) {
            feeNoteVo.setEndDate(DateUtils.addDays(feeNoteVo.getEndDate(), 1));
        }
        if (StringUtils.isNotBlank(startBeeDateWithoutDay)) {
            feeNoteVo.setStartBeeDate(DateUtils.parseDate(startBeeDateWithoutDay, "yyyy-MM"));
        }
        if (StringUtils.isNotBlank(endBeeDateWithoutDay)) {
            feeNoteVo.setEndBeeDate(DateUtils.parseDate(endBeeDateWithoutDay, "yyyy-MM"));
        }
        page = feeNoteService.selectByDomainWhere(page, feeNoteVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    @RequestMapping("add")
    public String toAdd(HttpServletRequest request) {
        AuthorizationVO authorizationVO = SessionUserUtils.getSessionAttributeForUserDtl();
        StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(authorizationVO.getHrStaffId());
        request.setAttribute("staffName", staffInfo.getStaffName());
        //获取费用归属公司名称
        String visibleOrgIds = (String)SessionUserUtils.getSessionAttribute("visibleOrgIds");
        List<OrgInfo> orgInfos = new ArrayList<OrgInfo>();
        if (StringUtils.isEmpty(visibleOrgIds)) {
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setState(1);
            orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
        }
        else {
            orgInfos = orgInfoService.selectByVisibleIdsAndNotArea(visibleOrgIds);
        }
        request.setAttribute("orgInfos", orgInfos);
        return "fia/lay-feeNote-add";
    }

    /**
     * 费用登记-新增
     * @throws ParseException 
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(HttpServletRequest request, FeeNote feeNote, String monthWithoutDay,
                      String hrOrgBillId)
        throws ParseException {

        feeNote.setHrOrgBillId(hrOrgBillId);//费用归属公司id
        feeNote.setBeeDateMonth(DateUtils.parseDate(monthWithoutDay, "yyyy-MM"));

        feeNoteService.saveFeeNote(feeNote);

        return RspVo.getSuccessResponse();
    }

    /**
     * 费用登记-作废
     * @throws ParseException 
     */
    @RequestMapping("/cancel")
    @ResponseBody
    public Boolean cancel(HttpServletRequest request, String id) {
        Boolean flag = false;
        FeeNote feeNote = feeNoteService.selectByPrimaryKey(id);
        if (feeNote.getState() == 1) {
            flag = feeNoteService.changeState(id, 2);
        }
        if (feeNote.getState() == 2) {
            flag = feeNoteService.changeState(id, 1);
        }
        return flag;
    }

    /**
     * 费用登记-详情
     */
    @RequestMapping("/view")
    public ModelAndView details(HttpServletRequest request, String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        FeeNoteDomain feeNote = feeNoteService.queryDomainById(id);
        map.put("feeNoteDomain", feeNote);
        return new ModelAndView("fia/lay-feeNote-details", map);
    }

}
